LEFT JOIN
In this lesson, we will discuss the LEFT JOIN keyword.
We'll cover the following
LEFT JOIN#
The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL
from the right side if there is no match.
Syntax#
SELECT table1.column1, table2.column2...
FROM table1
LEFT JOIN table2
ON table1.common_field = table2.common_field;
Note: In some databases, LEFT JOIN is called LEFT OUTER JOIN.
Example#
We want to select all customers and any orders they might have placed:
The SQL query to retrieve all customers whether or not they have placed an order:
As you can see, the LEFT JOIN keyword returns all records from the left table (Customers), even if there are no matches in the right table (Orders).
Quick quiz!#
Will the following query return the customer NAME and ADDRESS along with the AMOUNT they purchased?
SELECT CUSTOMERS.NAME, CUSTOMERS.ADDRESS , ORDERS.AMOUNT
FROM CUSTOMERS
LEFT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
A)
True
B)
False
In the next lesson, we will discuss the RIGHT JOIN keyword.